home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5673 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  66 lines

  1. Path: charnel.ecst.csuchico.edu!mcelroy
  2. From: mcelroy@ecst.csuchico.edu (James Robert McElroy)
  3. Newsgroups: comp.lang.c++
  4. Subject: Nested Template Classes
  5. Date: 6 Feb 1996 04:18:29 GMT
  6. Organization: California State University, Chico
  7. Message-ID: <4f6kql$70s@charnel.ecst.csuchico.edu>
  8. NNTP-Posting-Host: hairball.ecst.csuchico.edu
  9.  
  10. Here's a real brain-twister for you, something hiding
  11. off in the dark recesses of C++dom.
  12.  
  13. What does the function definition for a nested template
  14. class function look like?  IS there such a construct?  I have
  15. not been able to find one that works (except for inlining
  16. every nested template class function).
  17.  
  18. e.g.   Normal:
  19.  
  20. class list {
  21.     private:
  22.        class node {
  23.       private:
  24.          int data;
  25.          node * next;
  26.           public:
  27.          node(int someData);
  28.          int getData();
  29.          etc...
  30.         };
  31.     public:
  32.       whatever...
  33.  
  34. };
  35.  
  36. int list::node::getData() { return data; }  // okay -- correct form
  37.  
  38.  
  39. Templated:  take the above class definition and templatize
  40.         it, replacing the type of node::data with the
  41.         template parameter "T" instead of "int".
  42.  
  43.             Now, assuming you don't want to inline the 
  44.         getData method, how do you define it?
  45.  
  46. template <class T>
  47. T list<T>::node<T>::getData() { return data; }  // doesn't work
  48.  
  49. template <class T>
  50. T list<T>::node::getData() { return data; }     // doesn't work
  51.  
  52. template <class T>
  53. T list::node<T>::getData() { return data; }     // doesn't work
  54.  
  55. template <class T>
  56. T list::node::getData() { return data; }        // doesn't work, 
  57.                                of course.
  58.  
  59.  
  60. The compiler barfs on all of these.      
  61.  
  62. -- 
  63. Jim McElroy
  64. Calif. State Univ., Chico
  65. mcelroy@ecst.csuchico.edu
  66.